﻿using System;
using Pay.DTO;
using Pay.Core;
using System.Collections.Generic;
using System.Threading.Tasks;
using Flurl.Http;
using System.Net.Http;
using System.Text;
using Pay.DTO.htl;

namespace Pay.Service {
	public class HtlMallSign {
#if DEBUG
        private const string alliance_code = "LGCWCROLB9EBDCTD";
        private const string alliance_key = "dfba2e4bdf001a301bad6177a9093cbc";
        private const string req_url = "http://220.249.191.230:60606/apis/alliances{0}";
#else
        private const string alliance_code = "BPVBMWX7LK6JNCH3";
        private const string alliance_key = "734fda8bf285968e57ca4db0ab2ce505";
        private const string req_url = "http://www.haitaole.com.cn/apis/alliances{0}";
#endif
        public HtlMallSign() { }
        public async Task<T> HtlReq<T>(htl_req_base model, string url) where T : htl_res_base, new() {
            var log = IocHelper.Get<ILogHelper>("info");
            var content = _sign(model);
            log.Info(content.ToJson());
            try {
                var formUrlContent = new FormUrlEncodedContent(content);
                var resp1 = await string.Format(req_url,url).PostAsync(formUrlContent).ReceiveString();
                log.Info(resp1);
                return resp1.ToXmlObject<T>();
            } catch (FlurlHttpException ex) {
                log.Error(ex.Message);
                return null;
            }
        }

        private Dictionary<string, string> _sign(htl_req_base model) {
            model.alliance_code = alliance_code;
            var log = IocHelper.Get<ILogHelper>("info");
            var content = new Dictionary<string, string>();
            string json = model.ToJson();
            log.Info(json);
            var collection = json.ToObject<Dictionary<string, object>>();
            collection.Remove("sign");
            foreach (var kv in collection) {
                if (kv.Value != null && !string.IsNullOrEmpty(kv.Value.ToString())) {
                    content.Add(kv.Key, kv.Value.ToString());
                }
            }
            string strTxt = string.Format("{0}|{1}|{2}", model.alliance_code, model.request_time, alliance_key);
            string sign = SecureHelper.MD5(strTxt);
            log.Info(sign);
            content.Add("sign", sign);
            return content;
        }
        /// <summary>
        /// 验签
        /// </summary>
        /// <param name="req"></param>
        /// <returns></returns>
        public bool VerifySign(htl_req_base req) {
            var log = IocHelper.Get<ILogHelper>("info");
            bool result = false;
            string strText = string.Format("{0}|{1}|{2}", req.alliance_code, req.request_time, alliance_key);
            string md5 = SecureHelper.MD5(strText).ToLower();
            result = (req.sign.ToLower() == md5);
            return result;
        }
        /// <summary>
        /// 验证成功
        /// </summary>
        /// <param name="res"></param>
        /// <returns></returns>
        public string VerifySuccess() {
            var res = new response {
                alliance_code = alliance_code,
                request_time = DateTime.Now.ToString("yyyy-MM-dd hh:mm:ss"),
                status = 1
            };
            string strText = string.Format("{0}|{1}|{2}|{3}", res.alliance_code, res.request_time, res.status, alliance_key);
            res.sign = SecureHelper.MD5(strText);
            return XmlHelper.XmlSerialize(res, Encoding.UTF8, false);
        }
        /// <summary>
        /// 验证失败
        /// </summary>
        /// <param name="error_msg"></param>
        /// <returns></returns>
        public string VerifyFail(string error_msg) {
            var res = new response {
                alliance_code = alliance_code,
                request_time = DateTime.Now.ToString("yyyy-MM-dd hh:mm:ss"),
                error = error_msg,
                status = 2
            };
            string strText = string.Format("{0}|{1}|{2}|{3}|{4}", res.alliance_code, res.request_time, res.status, res.error, alliance_key);
            res.sign = SecureHelper.MD5(strText);
            return XmlHelper.XmlSerialize(res, Encoding.UTF8, false);
        }
    }
}
